﻿using System;
using System.Data;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using log4net;
using System.Reflection;
using AnyOffice.DAL;
using AnyOffice.BIZ;
using AnyOffice.Components;
using AnyOffice.general.file_folder;

namespace AnyOffice.module.attach
{
    public partial class save_file : BasePage
    { 
        // Fields
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                string attachmentId = (Request.QueryString["attachment_id"] == null) ? "" : Request.QueryString["attachment_id"].ToString();
                this.ViewState["AttachmentId"] = attachmentId;
                string str2 = (Request.QueryString["name"] == null) ? "" : Request.QueryString["name"].ToString();
                this.ViewState["FileName"] = str2;
                this.litFileName.Text = str2;
                this.txtFileName.Text = str2;
                if ((attachmentId == "") || (str2 == ""))
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("文件不存在");
                    return;
                }
                if (!AttachmentBiz.Contains(attachmentId))
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("无访问权限");
                    return;
                }
                if (!File.Exists(Server.MapPath(AttachmentBiz.AttachmentPath) + @"\" + attachmentId + @"\" + str2))
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("文件不存在");
                    return;
                }
                DataTable dtbDirectory = new LiteUtility().GetDtbDirectory();
                this.currentFolder.DataSource = dtbDirectory;
                this.currentFolder.DataTextField = "path";
                this.currentFolder.DataValueField = "path";
                this.currentFolder.DataBind();
            }
            if (this.Page.IsPostBack)
            {
                try
                {
                    string sourceFileName = Server.MapPath(AttachmentBiz.AttachmentPath) + @"\" + this.ViewState["AttachmentId"].ToString() + @"\" + this.ViewState["FileName"].ToString();
                    string path = HttpContext.Current.Server.MapPath("/attachment/file_folder/" + HttpContext.Current.User.Identity.Name + ((this.currentFolder.SelectedValue == "/") ? "" : this.currentFolder.SelectedValue));
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    string str6 = path + @"\" + this.ViewState["FileName"].ToString();
                    string str7 = str6;
                    for (int i = 1; File.Exists(str7); i++)
                    {
                        str7 = str6.Substring(0, str6.LastIndexOf(".")) + i + str6.Substring(str6.LastIndexOf("."));
                    }
                    File.Copy(sourceFileName, str7, true);
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("文件转存成功");
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowErrorClose("文件转存失败");
                }
            }


        }
    }
}
